const app = getApp()
const GetPeriod = require("../../utils/getperiod.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab:1,
    showModal:false,
    userphone:false,
    hidden:true,
  },
  preventTouchMove() {},
  changeTab(e){
    var tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab:Number(tab),
    })
  },

  // 查看
  select() {
    var that = this;
    that.request()
    
  },

  // 查看
  toPage(e) {
    console.log(e)
    var type = e.currentTarget.dataset.type;
    var count = e.currentTarget.dataset.count;  //  总数
    if (type == 0){
        // 今日预约单
        wx.navigateTo({
          url: `/pages/order/index?type=` + type 
        })
    } else if (type == 1){
        //今日应拍
        wx.navigateTo({
          url: `/pages/photograph/index?type=` + type
        });
    } else if (type == 2){
        //今日应选样   
        wx.navigateTo({
          url: `/pages/sample/index?type=` + type
        });
    } else if (type == 3){
        //看精修
        wx.navigateTo({
          url: `/pages/truing/index?type=` + type
        });
    } else if (type == 4){
        //今日应取件   
        wx.navigateTo({
          url: `/pages/pickup/index?type=` + type
        });
    } else if (type == 5){
        //营销明细 
        var typetab = e.currentTarget.dataset.typetab;    
        console.log(typetab)
        wx.navigateTo({
          url: `/pages/businessDetail/index?type=` + type + `&typetab=`+ typetab
        })
    }
    
  },
 
     
  toggleModal(){
    this.setData({
        showModal: !this.data.showModal
    })
    if(this.data.showModal){
        wx.hideTabBar();
    }else{
        wx.showTabBar();
    }
  },

  changeMenu(e){
    var that =this
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var start_shop = { id: id, name: name }
    wx.setStorageSync('start_shop_', start_shop); 
    this.setData({
        showModal: false
    })
    wx.showTabBar();
    that.request()
  },

  // 加载组件
  circle(e) {
    this.selectAllComponents('.circle').forEach(function (item, index) {
      item.startDrawing()
    })
  },


// 选择结束时间
 endDateTime(e){
    var that = this
    var start_time = new Date(app.cache_read_startTime()).getTime();
    var end_time = new Date(e.detail.value).getTime();
    var timestamp = Date.parse(new Date());  
    if(app.date_judge(start_time,end_time,timestamp) === false){
        return false;
    }
    that.setData({
      end: e.detail.value,
    })
    app.cache_write_endTime(e.detail.value)

 },
// 选择开始时间
  startDateTime(e) {
    var that = this
    var start_time = new Date(e.detail.value).getTime();
    var end_time = new Date(app.cache_read_endTime()).getTime();
    var timestamp = Date.parse(new Date());  
    if(app.date_judge(start_time,end_time,timestamp) === false){
        return false;
    }
    that.setData({
      start: e.detail.value,
    })
    app.cache_write_startTime(e.detail.value);
  },
// 今日
  now(e){
    var that = this;
    that.time = new GetPeriod();
    let now = that.time.getNowDate(); //今天日期
    var tab = e.currentTarget.dataset.tab;
    app.cache_write_startTime(now);
    app.cache_write_endTime(now);
    that.setData({
      activeTab: Number(tab),
    })
    that.request()
  },
// 本周
  weeks(e){
    var that = this;
    that.time = new GetPeriod();
    let start = that.time.getWeekStartDate(); 
    let end = that.time.getWeekEndDate();
    var tab = e.currentTarget.dataset.tab;
    app.cache_write_startTime(start);
    app.cache_write_endTime(end);
    that.setData({
      activeTab: Number(tab),
    })
    that.request()
  },
// 本月
  month(e){
    var that = this;
    that.time = new GetPeriod();
    let yuestart = that.time.getMonthStartDate(); 
    let yueend = that.time.getMonthEndDate(); 
    var tab = e.currentTarget.dataset.tab;
    app.cache_write_startTime(yuestart);
    app.cache_write_endTime(yueend);
    that.setData({
      activeTab: Number(tab),
    })
    that.request()
  },


  changeTab(e){
    var tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab:Number(tab),
    })
  },

  // 请求方法
  request() {
    var that = this
    var start = app.cache_read_startTime(); 
    var end = app.cache_read_endTime(); 
    var start_shop = wx.getStorageSync('start_shop_');
    var all_start_shop = wx.getStorageSync('all_start_shop_');
    console.log(start,end,all_start_shop)
    that.setData({
      hidden: false,
      userphone: true,
      start_shop: start_shop,
      all_start_shop: all_start_shop.shops,
    })
    wx.request({
      url: app.https() + 'index',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        start_time: start,
        end_time: end,
        shop_id: start_shop.id,
        company_id: all_start_shop.company?all_start_shop.company.id:'',
      },
      success: function (res) {
          console.log(res)
        if (res.data.resCode == 2001) {
          that.setData({
            res: res.data.resData,
            hidden: true,
            userphone: true,
            start: start,
            end: end,
            })
          that.circle()
        } else {
          console.log(res)
        }
      }
    })

  },
  onReady: function(){
    this.selectAllComponents('.circle').forEach(function(item,index){
       
        item.startDrawing()
    })
  },

  /*
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  = this
    that.time = new GetPeriod();
    let start = that.time.getWeekStartDate(); // 本周开始日期
    let end = that.time.getWeekEndDate(); // 本周结束日期
    app.cache_write_startTime(start);
    app.cache_write_endTime(end);
    
    that.setData({
      // start: start,
      // end: end,
      activeTab: Number(1),
    })
     
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var userphone = wx.getStorageSync('userphone_');
    var all_start_shop = wx.getStorageSync('all_start_shop_');
    // 无电话登录授权页面
    if (!userphone){
        wx.showToast({
          title: "请先登录！",
          icon: 'none',
          duration: 2000
        });
      that.setData({
        userphone: false,
      })
        setTimeout(function () {
          wx.switchTab({
            url: '../my/index', 
              })
        }, 2000)
        return false;
      }

    if (!all_start_shop.company) {
      wx.showToast({
        title: "暂无数据！",
        icon: 'none',
        duration: 2000
      });
      that.setData({
        userphone: false,
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../my/index',
        })
      }, 2000)
      return false;
    }
    wx.stopPullDownRefresh();
    that.request()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})